package com.it.mydream.Controller;

import com.it.mydream.Bean.MsgBoard;
import com.it.mydream.Bean.User;
import com.it.mydream.Service.imp.LoginServiceImp;
import com.it.mydream.Service.imp.MsgBoardServiceImp;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    private LoginServiceImp loginService;
    @Autowired
    private ShaoleiController slController;
    @Autowired
    private MsgBoardServiceImp msgBoardService;

    @RequestMapping("gonewmain")
    public String gonewmain(HttpServletRequest request){
        //更新用户状态
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20003");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        List<User> userlist = loginService.getUserList();
        application.setAttribute("userlist",userlist);

        //获取留言板信息
        List<MsgBoard> msgBoards = (List<MsgBoard>) application.getAttribute("msgBoards");
        if(msgBoards==null){
            msgBoards = msgBoardService.GetMsgBoards();
            application.setAttribute("msgBoards",msgBoards);
        }
        return "main/main";
    }

    @RequestMapping("goVideo")
    public String goVideo(HttpServletRequest request){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20005");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        List<User> userlist = loginService.getUserList();
        application.setAttribute("userlist",userlist);

        session.setAttribute("talklist",null);
        return "main/video";
    }

    @RequestMapping("goHall")
    public String goHall(HttpServletRequest request){
        //更新用户状态
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20003");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        //获取留言板信息
        ServletContext application = request.getServletContext();
        List<MsgBoard> msgBoards = (List<MsgBoard>) application.getAttribute("msgBoards");
        if(msgBoards==null){
            msgBoards = msgBoardService.GetMsgBoards();
            application.setAttribute("msgBoards",msgBoards);
        }
        return "hall";
    }

    @RequestMapping("goChangeUserImg")
    public String goChangeUserImg(){
        return "changeuserimg";
    }


    @RequestMapping("goShaolei")
    public String goShaolei(HttpServletRequest request){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20004");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        List<User> userlist = loginService.getUserList();
        application.setAttribute("userlist",userlist);

        slController.createShaoleiTable(request);
        return "game/shaolei";
    }

    @RequestMapping("goDouChong")
    public String goDouChong(HttpServletRequest request){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20008");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        List<User> userlist = loginService.getUserList();
        application.setAttribute("userlist",userlist);

        slController.createShaoleiTable(request);
        return "game/douchong";
    }

    @RequestMapping("backMain")
    public String backMain(HttpServletRequest request){
        return gonewmain(request);
    }

    @RequestMapping("gologout")
    public String goLogout(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20002");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);
        session.removeAttribute("user");
        model.addAttribute("user",new User());

        List<User> userlist = loginService.getUserList();
        application.setAttribute("userlist",userlist);
        return "login";
    }
}
